package com.qianyu.api.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qianyu.core.entity.base.BaseIdDto;
import com.qianyu.core.entity.base.Result;
import com.qianyu.core.entity.dto.QqRentOrderDto;
import com.qianyu.core.entity.vo.QqRentOrderVo;
import com.qianyu.core.helper.LoginHelper;
import com.qianyu.core.service.QqRentOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

@Api(tags = "前台管理-订单中心", description = "wangzhicheng")
@RestController
@RequestMapping("/front/rentOrder")
@RequiredArgsConstructor
public class QqRentOrderController {

    private final QqRentOrderService qqRentOrderService;

    private final RedissonClient redissonClient;

    /**
     * 获取租用金额
     */
    @PostMapping("/getAmount")
    @ApiOperation(value = "获取租用金额")
    Result<BigDecimal> getAmount(@RequestBody @Validated QqRentOrderDto.RentDto dto) {
        return Result.success(qqRentOrderService.getAmount(dto));
    }

    /**
     * 租号
     */
    @PostMapping("/addOrder")
    @ApiOperation(value = "租号")
    Result<String> addOrder(@RequestBody @Validated QqRentOrderDto.RentDto dto) {
        RLock lock = redissonClient.getLock(LoginHelper.getUsername());
        try {
            // 上锁
            lock.lock();
            qqRentOrderService.addOrder(dto);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        } finally {
            if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                lock.unlock();
            }
        }

        return Result.success();
    }

    /**
     * 续期
     */
    @PostMapping("/continueOrderAccount")
    @ApiOperation(value = "续期")
    Result<String> continueOrderAccount(@RequestBody @Validated QqRentOrderDto.RentContinueDto dto) {
        RLock lock = redissonClient.getLock(LoginHelper.getUsername());
        try {
            // 上锁
            lock.lock();
            qqRentOrderService.continueOrderAccount(dto);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        } finally {
            if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                lock.unlock();
            }
        }

        return Result.success();
    }

    /**
     * 服务商分页查询租号订单
     */
    @PostMapping("/pageList")
    @ApiOperation(value = "服务商分页查询租号订单")
    Result<Page<QqRentOrderVo.RentOrderPage>> pageList(@RequestBody @Validated QqRentOrderDto.RentOrderPageDto dto) {
        return Result.success(qqRentOrderService.pageList(dto));
    }

    /**
     * 根据订单id获取订单详情
     */
    @PostMapping("/getInfo")
    @ApiOperation(value = "根据订单id获取订单详情")
    Result<QqRentOrderVo.RentOrderInfo> getInfo(@RequestBody @Validated BaseIdDto dto) {
        return Result.success(qqRentOrderService.getInfo(dto));
    }
}
